//
//  WHNTopListCollectionViewCell.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WHNTopListCollectionViewCell.h"
#import "WHNTitleCollectionReusableView.h"
#import "WHNCollectionViewCell.h"
#import "WHNTopList.h"
#import "FYPageScrollView.h"
#import "AFTools.h"
#import <UIImageView+WebCache.h>
#import "FitHeader.h"

@implementation WHNTopListCollectionViewCell

#pragma mark 加载图
-(MBProgressHUD *)progressView
{
    if (!_progressView) {
        _progressView = [[MBProgressHUD alloc]initWithView:self];
        _progressView.labelText = @"加载中";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        [_progressView NightWithType:UIViewColorTypeNormal];
    }
    return _progressView;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.WHNTopListCollectionView];
        [self getData];
        [self addSubview:self.progressView];
    }
    return self;
}
#pragma mark 获取数据
-(void)getData
{
    _WHNList = [NSMutableArray array];
    _WHNArr = [NSMutableArray array];
    [AFTools getDataByURLString:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=yjf-22&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.20.2" WithDataBlock:^(id data) {
        NSArray *dataArray = [data objectForKey:@"datas"];
        _WHNArr = [[data objectForKey:@"focusImages"]objectForKey:@"list"];
        NSArray *listArray = [dataArray[0] objectForKey:@"list"];
        NSMutableArray *array1 = [NSMutableArray array];
        for (NSDictionary *listDic in listArray) {
            WHNTopList *top = [[WHNTopList alloc]init];
            [top setValuesForKeysWithDictionary:listDic];
            [array1 addObject:top];
        }
        NSArray *listArr = [dataArray[1] objectForKey:@"list"];
        NSMutableArray *array2 = [NSMutableArray array];
        for (NSDictionary *dic in listArr) {
            WHNTopList *top = [[WHNTopList alloc]init];
            [top setValuesForKeysWithDictionary:dic];
            [array2 addObject:top];
        }
        [_WHNList addObject:array1];
        [_WHNList addObject:array2];
        [_WHNTopListCollectionView reloadData];
        [_progressView removeFromSuperview];
    }];
}
#pragma mark 懒加载 CollectionView初始化
-(UICollectionView *)WHNTopListCollectionView
{
    if (!_WHNTopListCollectionView) {
        UICollectionViewFlowLayout *LayOut = [[UICollectionViewFlowLayout alloc]init];
        LayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
        LayOut.minimumLineSpacing = 0;
        LayOut.minimumInteritemSpacing = 0;
        LayOut.sectionInset = UIEdgeInsetsMake(0, 10 * FitWidth, 10 * FitHeight, 10 * FitWidth);
        _WHNTopListCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:LayOut];
        _WHNTopListCollectionView.dataSource = self;
        _WHNTopListCollectionView.delegate = self;
        [_WHNTopListCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head1"];
        [_WHNTopListCollectionView registerClass:[WHNTitleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head2"];
        [_WHNTopListCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head3"];
        [_WHNTopListCollectionView registerClass:[WHNTitleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head4"];
        [_WHNTopListCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"list1"];
        [_WHNTopListCollectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:@"list2"];
        [_WHNTopListCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"list3"];
        [_WHNTopListCollectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:@"list4"];
        [_WHNTopListCollectionView NightWithType:UIViewColorTypeNormal];
    }
    return _WHNTopListCollectionView;
}
#pragma mark - collectionView 方法
#pragma mark cell的样式
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"list1" forIndexPath:indexPath];
        if (cell.subviews.count == 1) {
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *list in _WHNArr) {
                [array addObject:[list objectForKey:@"pic"]];
            }
            FYPageScrollView *view = [[FYPageScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 180 * FitHeight) PicArray:array];
            [cell addSubview:view];
        }
        return cell;
    }
    if (indexPath.section == 1) {
        WHNCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"list2" forIndexPath:indexPath];
        cell.kind = WHNCollectionViewKindThreeLabelAndTwoPicture;
        if (_WHNList.count != 0) {
            [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[_WHNList[0][indexPath.row] coverPath]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
            cell.mainImageView.layer.cornerRadius = 5;
            cell.mainImageView.layer.masksToBounds = YES;
            cell.titleLabel.text = [_WHNList[0][indexPath.row] title];
            cell.byLabel.text = [NSString stringWithFormat:@"1 %@", [[_WHNList[0][indexPath.row] firstKResults][0]objectForKey:@"title"]];
            cell.longLabel.text = [NSString stringWithFormat:@"2 %@", [[_WHNList[0][indexPath.row] firstKResults][1]objectForKey:@"title"]];
            cell.rightImageView.image = [UIImage imageNamed:@"xiangyou"];
        }
        return cell;
    }
    if (indexPath.section == 2) {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"list3" forIndexPath:indexPath];
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
    }
    
    WHNCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"list4" forIndexPath:indexPath];
    cell.kind = WHNCollectionViewKindThreeLabelAndTwoPicture;
    if (_WHNList.count != 0) {
        [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[_WHNList[1][indexPath.row] coverPath]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
        cell.mainImageView.layer.cornerRadius = 5;
        cell.mainImageView.layer.masksToBounds = YES;
        cell.titleLabel.text = [_WHNList[1][indexPath.row] title];
        cell.byLabel.text = [NSString stringWithFormat:@"1 %@", [[_WHNList[1][indexPath.row] firstKResults][0]objectForKey:@"title"]];
        cell.longLabel.text = [NSString stringWithFormat:@"2 %@", [[_WHNList[1][indexPath.row] firstKResults][1]objectForKey:@"title"]];
        cell.rightImageView.image = [UIImage imageNamed:@"xiangyou"];
    }
    return cell;
}
#pragma mark 每个分区中 item 个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else if (section == 1) {
        return 3;
    }
    else if (section == 2) {
        return 1;
    }
    else {
        return 3;
    }
}
#pragma mark 分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    //数据保护,最先走,无值即退出不会走其他方法
    if (_WHNArr.count == 0 || _WHNList.count == 0) {
        return 0;
    }
    return 4;
}
#pragma mark 每个分区中 item 大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(self.frame.size.width, 180 * FitHeight);
    }
    else if (indexPath.section == 2) {
        return CGSizeMake(self.frame.size.width, 10 * FitHeight);
    }
    else {
        return CGSizeMake(self.frame.size.width, 100 * FitHeight);
    }
}
#pragma mark 头视图大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == 2) {
        return CGSizeMake(0, 0);
    }
    else {
        return CGSizeMake(self.frame.size.width, 40 * FitHeight);
    }
}
#pragma mark 头视图样式
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        WHNTitleCollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head1" forIndexPath:indexPath];
        return head;
    }
    else if (indexPath.section == 1) {
        WHNTitleCollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head2" forIndexPath:indexPath];
        head.WHNLeftImageView.image = [UIImage imageNamed:@"sanjiao"];
        head.WHNHeadLabel.text = @"节目榜单";
        return head;
    }
    else if (indexPath.section == 2) {
        WHNTitleCollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head3" forIndexPath:indexPath];
        return head;
    }
    else {
        WHNTitleCollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head4" forIndexPath:indexPath];
        head.WHNLeftImageView.image = [UIImage imageNamed:@"sanjiao"];
        head.WHNHeadLabel.text = @"主播榜单";
        return head;
    }
}
#pragma mark 点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 1:
            [self.delegate passKey:[_WHNList[0][indexPath.row] key] Type:[_WHNList[0][indexPath.row] contentType]];
            break;
        case 3:
            [self.delegate passKey:[_WHNList[1][indexPath.row] key] Type:[_WHNList[1][indexPath.row] contentType]];
            break;
            
        default:
            break;
    }
}

















@end
